package Api;


import Util.HttpUtil;
import Util.MyKey;
import Util.SignEncrypt;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import org.bouncycastle.util.encoders.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;

import static Util.Tools.parseJsonResponse;

/**
 *  非支付凭证在此处下载
 */

public class ReceiptGet {
    public static void main(String[] args) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String time = sdf.format(now);

        //公共参数
        String method = "heepay.com.receipt.get";
        String version ="1.0";
        String merch_id ="1664502";
        String timestamp =time;
        //业务参数

        //凭证类型（1=分账,2=分账退回,3=银行卡充值,4=动账储值,5=线下汇款,6=提现,7=批付,8=批付批量获取）
        String receipt_type ="1";
        String trade_no ="H2311131893766AW";
        String sub_merch_id ="";

        JSONObject biz_content = new JSONObject();
        biz_content.put("receipt_type",receipt_type);
        biz_content.put("trade_no",trade_no);
        //biz_content.put("sub_merch_id",sub_merch_id);

        //签名串组装
        String OldSign = "biz_content=" + biz_content
                + "&merch_id=" + merch_id
                +"&method=" + method
                +"&timestamp=" + timestamp
                +"&version=" + version;

        System.out.println("签名串：--------->"+OldSign);
        System.out.println();

        SignEncrypt signEncrypt = new SignEncrypt();

        String privateKeystr = MyKey.privateKeystr;

        //密钥配置
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] privateKeyPath = Base64.decode(privateKeystr.getBytes());
        PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyPath));
        //加签
        String sign = signEncrypt.sign(OldSign, privateKey);


        //最终请求数据
        JSONObject param = new JSONObject();
        param.put("method",method);
        param.put("version",version);
        param.put("merch_id",merch_id);
        param.put("timestamp",timestamp);
        param.put("biz_content",biz_content);
        param.put("sign",sign);

        System.out.println("最终请求数据：--------->"+param);
        System.out.println();

        String a = HttpUtil.sendJsonHttpPost("https://pay.heepay.com/API/HywIndex.aspx", String.valueOf(param));

        System.out.println("请求返回参数：--------->"+a);
        System.out.println();

        //解析返回参数
        JsonNode jsonNode = parseJsonResponse(a);
        String msg = jsonNode.get("msg").asText();

        if ( "调用成功".equals(msg) ){
            //解析data参数
            String data = jsonNode.get("data").asText();
            JsonNode jsonNode1 = parseJsonResponse(data);
            //获取凭证下载地址
            String file_path = jsonNode1.get("file_path").asText();
            System.out.println("输出凭证下载地址：--->"+file_path);
        }else {
            String sub_msg = jsonNode.get("sub_msg").asText();
            System.out.println("输出失败sub_msg：--------->"+sub_msg);
        }

    }
}
